;(function(){
    zUI.dialog={
        maskCount:0,
        alertUI:function(set){
            var dom = zUI.dom;
            var title = set.title||'提示';
            var width = set.width;
            var height = set.height;
            var close = set.close;
            var beforeClose = set.beforeClose;
            var draggable = set.draggable !== false;
            var resizable = set.resizable !== false;
            var shadow = set.shadow !== false;
            var btns = set.btns||null;
            var id = set.id||('zUI_'+zUI.util.UUID());
            if($('#'+id).length > 0){
                return false;
            }
            var shadowDiv = null;
            if(shadow){
                shadowDiv = dom.createElem({name:'div',clazz:'zUI-shadow',id:'mark_'+id});
                shadowDiv.style.display='none';
                document.body.appendChild(shadowDiv);
                $(shadowDiv).fadeIn('fast');
            }
            var content = set.content||'';
            var obj = dom.createElem({name:'div',clazz:'zUI-box'+(set.ui===false? ' alert':''),id:id});
            obj.style.display='none';

            var tit = dom.createElem({name:'div',clazz:'zUI-title no-select'+ (draggable?' draggable':'')});

            obj.appendChild(tit);
            var h1 = dom.createElem({name:'h1',clazz:'zUI-title',text:title});

            tit.appendChild(h1);

            var dv2 = document.createElement('div');
            var clo = dom.createElem({name:'a',text:'×'});
            dv2.appendChild(clo);
            tit.appendChild(h1);
            tit.appendChild(dv2);

            var cnt = dom.createElem({name:'div',clazz:'zUI-content'});
            if(set.ui === false){
                cnt.style.minHeight = '86px';
                obj.style.minHeight = '173px';
            }

            obj.appendChild(cnt);
            if(typeof content === 'string'){
                cnt.innerHTML = content;
            }else{
                $(cnt).append(content);
            }
            var btnBox = dom.createElem({name:'div',clazz:'zUI-btns'});
            if(btns !== null){
                obj.appendChild(btnBox);
            }else{
                dom.addClass(cnt,'not-btn');
            }

            document.body.appendChild(obj);
            $(obj).fadeIn(100);
            if(height){
                obj.style.height=height;
            }
            if(width){
                obj.style.width=width;
            }
            function isF(f){
                return typeof f === 'function';
            }
            var btnArr = [];
            if(btns !== null){
                for(var bn in btns){
                    var f = btns[bn];
                    //防止循环结束后，回调时bn永远是最后一个值
                    var b = dom.createElem({name:'a',clazz:'zUI-btn',text:bn});
                    b.setAttribute('href','javascript:;');
                    btnBox.appendChild(b);
                    if(isF(f)){
                        b.onclick = function(ff){
                            return function () {
                                if(ff() !== false){
                                    var doClose = true;
                                    if(zUI.util.isF(beforeClose)){
                                        doClose = beforeClose();
                                    }
                                    if(doClose !== false){
                                        /*$(shadowDiv).fadeOut('fast',function () {
                                            $(shadowDiv).remove();
                                        });
                                        $(obj).fadeOut('fast',function () {
                                            $(obj).remove();
                                        });*/
                                        $(obj).remove();
                                        $(shadowDiv).remove();
                                        if(zUI.util.isF(close)){
                                            close();
                                        }
                                    }

                                }
                            }
                        }(f);
                    }
                    btnArr.push(b);
                }
            }

            if(draggable){
                tit.onmousedown=function(e){
                    var e = e || window.event;
                    var state = 1;
                    var x0 = e.clientX - obj.offsetLeft;
                    var y0 = e.clientY - obj.offsetTop;
                    /*if(typeof tit[0].setCapture !== 'undefined'){
                        tit[0].setCapture();
                    }*/
                    var f1 = function(e1){
                        e1 = e1 || window.event;
                        if(state == 1){
                            var moveX = e1.clientX - x0;
                            var moveY = e1.clientY - y0;
                            if(moveX < 0){
                                moveX = 0
                            }else if(moveX > window.innerWidth - tit.offsetWidth){
                                moveX = window.innerWidth - tit.offsetWidth
                            }
                            if(moveY < 0){
                                moveY = 0
                            }else if(moveY > window.innerHeight - tit.offsetHeight){
                                moveY =  window.innerHeight - tit.offsetHeight
                            }
                            obj.style.left = moveX + 'px';
                            obj.style.top = moveY + 'px'
                        }
                    }
                    var f2 = function(e2){
                        state = 0;
                        dom.removeEvent(document,"mousemove",f1);
                        dom.removeEvent(document,"mouseup",f2);
                    }
                    document.onmousemove = f1;
                    document.onmouseup = f2;

                };
            }
            if(resizable){
                var tool = dom.createElem({name:'span',clazz:'zUI-resize no-select'});
                obj.appendChild(tool);
                tool.onmousedown=function(e){
                    var e = e || window.event;
                    var state = 1;
                    var ww = $(obj).outerWidth();
                    var hh = $(obj).outerHeight();
                    var ccnntt = parseInt(cnt.style.height||cnt.offsetHeight);
                    var x0 = e.clientX;
                    var y0 = e.clientY;
                    var f1 = function(e1){
                        e1 = e1 || window.event;
                        if(state == 1){
                            e.preventDefault();
                            var x1 = e1.clientX;
                            var y1 = e1.clientY;
                            if(x1 < 0){
                                x1 = 0;
                            }else if(x1 > window.innerWidth){
                                x1 = window.innerWidth;
                            }
                            if(y1 < 0){
                                y1 = 0;
                            }else if(y1 > window.innerHeight){
                                y1 =  window.innerHeight;
                            }

                            obj.style.width=cnt.style.width=(ww+x1-x0)+'px';
                            obj.style.height=(hh+y1-y0)+'px';
                            cnt.style.height=(ccnntt+y1-y0)+'px';
                        }
                    }
                    var f2 = function(e2){
                        state = 0;
                        dom.removeEvent(document,"mousemove",f1);
                        dom.removeEvent(document,"mouseup",f2);
                    }
                    document.onmousemove = f1;
                    document.onmouseup = f2;
                }
            }
            obj.style.display = 'block';
            if(btnArr.length > 0){
                var minWidth = 0;
                for (var i = 0; i < btnArr.length; i++) {
                    minWidth += $(btnArr[i]).outerWidth();
                }
                var p1 = parseInt($(btnBox).css('paddingLeft'));
                var p2 = parseInt($(btnBox).css('paddingRight'));
                // var wm = dom.getW(btnBox);
                obj.style.minWidth = (minWidth+p1+p2 + btnArr.length*8+5)+'px';
                cnt.style.minWidth = (minWidth+p1+p2 + btnArr.length*8+5)+'px';
            }
            function setPos(){
                var winW = document.documentElement.clientWidth;
                var winH = document.documentElement.clientHeight;
                //offsetWidth获取只能是显示的元素
                var rw = $(obj).width();
                var rh = $(obj).height()+42+45;
                obj.style.top = (winH/2-rh/2)+'px';
                obj.style.left = (winW/2-rw/2)+'px';
            }
            setPos();
            clo.onclick=function(){
                var doClose = true;
                if(zUI.util.isF(beforeClose)){
                    doClose = beforeClose();
                }
                if(doClose !== false){
                    $(shadowDiv).remove();
                    $(obj).remove();
                    if(zUI.util.isF(close)){
                        close();
                    }
                }

            };
            window.onresize=setPos;
        },
        alert:function(content,title,sett){
            var set = {
                content: content,
                title: title,
                resizable: false,
                //弹窗是否有最小高度
                ui: false,
                width: '300px',
                btns: {
                    '确定':function(){}
                },
            }
            if(sett){
                $.extend(set,sett);
            }
            this.alertUI(set);
        },
        alertDom: function (set) {
            var dom = zUI.dom;
            var target = $(set.target);
            if(target.length > 0){
                var id = "_" + new Date().getTime()+"_"+zUI.util.UUID();
                var temp = dom.createElem({name:'div',id:id,clazz:'zUI-hide'});

                target.after(temp);

                var oldClose = set.beforeClose;
                set.beforeClose = function(){
                    $(target).addClass('zUI-hide');
                    $(temp).after(target);
                    $(temp).remove();
                    if(zUI.util.isF(oldClose)){
                        oldClose();
                    }
                }

                target.removeClass('zUI-hide');

                set.content = target;
                this.alertUI(set);

            }
        },
        hoverTip: function (set) {
            var dom = zUI.dom;
            var dir = set.dir || 'left';
            var items = $(set.elem);
            if(items){
                var loadOne = function(index,obj){

                    var title = obj.getAttribute('zUI-title');
                    var stay = obj.getAttribute('zUI-title-stay')||'false'+'';
                    if(title){
                        var s1 = '#function:';
                        if(title.indexOf(s1)===0 && title.lastIndexOf('#')===title.length - 1){
                            var f = title.substring(10,title.length - 1);
                            title = eval(f+'('+obj+')');
                            if(typeof title !== 'string'){
                                title = title.outerHTML;
                            }
                        }
                        var id = 'zUI_tip_' + zUI.util.UUID();
                        obj.onmouseover = function(e){
                            var tip = $('#'+id);
                            if(tip.length > 0){
                                $(tip).remove();
                                return;
                            }
                            tip = dom.createElem({name:'div',clazz:'zUI-tip',html:title,id:id});
                            var x,y,af;
                            $(tip).addClass(dir);
                            var ofst = $(e.target).offset();
                            var doLeft,doRight,doUp,doDown;
                            doLeft = function(){
                                x = ofst.left;
                                y = ofst.top + $(e.target).outerHeight()/2;
                                af = function(){
                                    var dx = x-$(tip).outerWidth()-2;
                                    tip.style.top = (y- $(tip).outerHeight()/2) + 'px';
                                    tip.style.left = (dx) + 'px';
                                    if(dx < 0){
                                        tip.style.left = '0px';
                                        tip.style.width = x+'px';
                                        tip.style.top = (y- $(tip).outerHeight()/2) + 'px';

                                    }
                                }
                            }
                            doRight = function(){
                                x = ofst.left + $(e.target).outerWidth()+2;
                                y = ofst.top + $(e.target).outerHeight()/2;
                                af = function(){
                                    tip.style.top = (y-$(tip).outerHeight()/2) + 'px';
                                    tip.style.left = (x-2) + 'px';
                                    var x1 = x + $(tip).outerWidth();
                                    var dx = $(document.body).width() - x1;
                                    if(dx < 0){
                                    }
                                }
                            }
                            doUp = function(){
                                x = ofst.left + $(e.target).outerWidth()/2;
                                y = ofst.top - 2;
                                af = function(){
                                    tip.style.top = (y - $(tip).outerHeight()) + 'px';
                                    tip.style.left = (x-$(tip).outerWidth()/2-2) + 'px';
                                }
                            }
                            doDown = function(){
                                x = ofst.left + $(e.target).outerWidth()/2;
                                y = ofst.top + $(e.target).outerHeight();
                                af = function(){
                                    tip.style.top = (y+ 2) + 'px';
                                    tip.style.left = (x-$(tip).outerWidth()/2-2) + 'px';
                                }
                            }
                            if(dir === 'left'){
                                doLeft();
                            }else if(dir === 'up'){
                                doUp();
                            }else if(dir === 'down'){
                                doDown();
                            }else{
                                doRight();
                            }
                            tip.style.left = x + 'px';
                            tip.style.top = y + 'px';
                            document.body.appendChild(tip);
                            af();
                            var key = zUI.util.UUID();
                            var clf = function(ee){
                                if(ee.target!== tip && !dom.isChild(ee.target,tip)){
                                    $(tip).remove();
                                    zUI.event.unbind(key);
                                }

                            };
                            zUI.event.bind(key,document,'click',clf);
                            document.onclick = clf;

                        }
                    }
                };
                zUI.util.each(items,loadOne);
            }
        },
        showMask: function (set) {
            var delay = parseInt(set&&set.delay);
            var maskCount = this.maskCount;
            this.maskCount = ++maskCount;
            var dom = zUI.dom;
            var mask = $('.zUI-mask');
            if(mask.length === 0){
                var hasText = set&&set.text;
                mask = dom.createElem({name:'div',clazz:'zUI-shadow zUI-mask'});

                mask.appendChild(dom.createElem({name:'div',clazz:'mask-box'}));
                mask.appendChild(dom.createElem({name:'div',clazz:'img'+(hasText?' hastext':'')}));
                var div = null;
                if(hasText){
                    div = dom.createElem({name:'div',clazz:'text'+(hasText?' hastext':''),html:set.text});
                    mask.appendChild(div);
                }
                document.body.appendChild(mask);
                if(div != null){
                    $(div).css('left','calc(50% - '+$(div).width()/2+'px)')
                }
            }
            var that = this;
            if(!isNaN(delay)){
                setTimeout(function () {
                    that.closeMask();
                },delay);
            }
        },
        closeMask: function (force) {
            var maskCount = this.maskCount;
            if(force === true){
                this.maskCount = 0;
            }else{
                this.maskCount = --maskCount;
            }
            if(maskCount <= 0){
                $('.zUI-mask').remove();
            }
        },
        message: function (set) {
            var dom = zUI.dom;
            var content = set.content||'';
            var dir = set.dir||'center';
            var ifClose = set.close !== false;
            var timer = set.timer;
            var delay = set.delay||'5000';
            if(delay < 1000){
                delay = 1000;
            }
            var level = set.level||'success';
            var queue = $('.zUI-message-queue.'+dir);
            if(queue.length == 0){
                queue = dom.createElem({name:'div',clazz:'zUI-message-queue '+dir});
                document.body.appendChild(queue);
            }else{
                queue = queue[0];
            }
            queue.style.width = 'auto';
            var msg = dom.createElem({name:'div',clazz:'zUI-message-wrap '+level});
            var iconName = {info:'fa-lightbulb-o',warn:'fa-info-circle',error:'fa-warning'}[level]||'fa-check';

            var icon1 = dom.createElem({name:'i',clazz:'zUI-icon fa '+iconName});

            msg.appendChild(icon1);
            var textDiv = dom.createElem({name:'div',text:content});
            msg.appendChild(textDiv);
            queue.appendChild(msg);
            if(dir === 'center'){
                msg.style.left = '-'+$(msg).outerWidth()/2+'px';
            }else if(dir === 'right'){
                var www = $(msg).outerWidth() + 6;
                msg.style.left = '-'+www+'px';
            }

            var ff = null;
            if(timer !== false){
                var icon2 = dom.createElem({name:'div',clazz:'zUI-message-timer'});
                var div = dom.createElem({name:'div'});
                icon2.appendChild(div);
                msg.appendChild(icon2);
                var ct = 60,stop = false;
                var divM = $(icon2).width();
                var divW = divM/(delay/ct-1);
                ff = setInterval(function () {
                    if(!stop){
                        var realW = $(div).width();
                        var nw = parseFloat(realW) + divW;
                        if(nw > divM){
                            nw = divM;
                        }
                        div.style.width = nw + 'px';
                    }
                },ct);
            }
            var closeTip = function(){
                if(ff != null){
                    clearInterval(ff);
                }
                var doF;
                var dy = $(msg).outerHeight()/4;
                var dxx = parseInt($(msg).outerWidth())/4;
                var opc = 1;
                if(dir ==='right'){
                    doF = function () {
                        var h1 = parseFloat($(msg).css('left'));
                        msg.style.left = (h1 + dxx)+'px';
                    };
                }else{
                    doF = function () {
                        var h1= 0;
                        msg.style.top = (h1 - dy)+'px';
                        msg.style.opacity = opc-=0.2;
                    };
                }

                var dh = setInterval(doF,50);
                setTimeout(function () {
                    clearInterval(dh);
                    $(msg).remove();
                },200);
            }
            var zmo = zUI.util.setTimeout(closeTip,delay);

            msg.onmouseover = function () {
                stop = true;
                zmo.pause();
            };
            msg.onmouseout = function () {
                stop = false;
                zmo.play();
            };
            if(ifClose){
                var close = dom.createElem({name:'span',clazz:'close',text:'×'});
                msg.appendChild(close);
                close.onclick = closeTip;
            }
            msg.style.width = $(msg).outerWidth() + 'px';
            var ms = queue.querySelectorAll('.zUI-message-wrap');
            var h = 0;
            var winH = document.documentElement.clientHeight - 20;
            zUI.util.each(ms,function (i,o) {
                h+= $(o).height()+5;
            });
            if(h > winH){
                $(ms[0]).remove();
            }
            queue.style.width = '0px';
        }
    }
}());